package com.example.wendao.model.db.dao;

import androidx.paging.DataSource;
import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import com.example.wendao.model.entity.Fans;
import com.example.wendao.model.entity.User;

@Dao
public interface FansDao {

    /**
     *
     * @param fans  建立两个用户之间的一个关注关系
     */
    @Insert
    void follow(Fans fans);

    /**
     *
     * @param beFollowerId  被关注者ID
     * @param followerId    关注者ID
     * @return              表示关注者关注了被关注者的关注关系的Fans对象
     */
    @Query("select * from fans where beFollowerId = :beFollowerId and followerId = :followerId")
    Fans queryIsFollow(int beFollowerId, int followerId);

    /**
     *
     * @param beFollowerId  被关注者ID
     * @return              关注者的用户列表的分页数据源  也就是粉丝列表
     */
    @Query("select * from users as u where userId in (select followerId from fans as f where beFollowerId = :beFollowerId)")
    DataSource.Factory<Integer, User> queryAllFans(int beFollowerId);

    /**
     *
     * @param followerId    关注者ID
     * @return              被关注者的用户列表的分页数据源
     */
    @Query("select * from users as u where userId in (select befollowerid from fans as f where followerId =:followerId)")
    DataSource.Factory<Integer, User> queryAllBeFollower(int followerId);

    /**
     *
     * @param beFollowerId  被关注者ID
     * @param followerId    关注者ID
     */
    @Query("update fans set followEachOther = 1 where beFollowerId = :beFollowerId and followerId = :followerId")
    void followEachOther(int beFollowerId ,int followerId);

    /**
     *
     * @param fans  要插入的粉丝对象序列
     */
    @Insert
    void insertFans(Fans[] fans);


    @Update
    void updateFan(Fans fan);

}
